# -*- coding:utf-8 -*-

import requests
from lxml import etree
from fake_useragent import UserAgent
from http import cookiejar
import json
from datetime import datetime
import time
import re
import csv
import os
from pymongo import MongoClient

from utils.HttpUtils import HttpUtils

requests.packages.urllib3.disable_warnings()  # 忽略HTTPS安全警告

"""
(.*?): (.*)
"$1":"$2",
"""


class TestRequest():
    def __init__(self):
        # 声明一个CookieJar对象实例来保存cookie
        # requests.utils.dict_from_cookiejar(html.cookies)  # 获取cookies转dict
        # res.cookies.get_dict()  # 获取cookies
        self.cookie = cookiejar.CookieJar()
        # self.ua = UserAgent(use_cache_server=False)  # 禁用服务器缓存
        # self.ua = UserAgent(verify_ssl=False)  #
        self.headers = {
            'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
            'Accept-Encoding': 'gzip, deflate',
            'Accept-Language': 'zh-CN,zh;q=0.9',
            'Connection': 'keep-alive',
            'Cookie': 'sessionid={79785c97-6322-4b5f-8a86-e31f7f260401}',
            'DNT': '1',
            'Host': 'cloud.mcu3.com',
            'Referer': 'http://cloud.mcu3.com/second.htm?a1=&a2=A2-%E6%96%9C&a3=&a4=&a5=&a6=&starttime=2022-02-19+22%3A05%3A46&endtime=2022-02-20+22%3A05%3A46&a7=500',
            'Upgrade-Insecure-Requests': '1',
            'User-Agent': 'Mozilla/5.0 (Linux; Android 5.1.1; TAS-AN00 Build/HUAWEIMLA-AL10; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/74.0.3729.136 Mobile Safari/537.36'
        }

    def get_contents(self, cursor):
        postData = {
            'securities': 'id:ACOR,include:true,,',

        }
        url = f"http://cloud.mcu3.com/second.htm?a1=&a2=A2-%E6%96%9C&a3=&a4=&a5=&a6=&starttime=2022-02-19+22%3A05%3A46&endtime=2022-02-20+22%3A05%3A46&a7=100000"
        html = HttpUtils.do_request("GET", url, self.headers, "")

        data_json = json.loads(html.text)

        for d in data_json['chart_data'][0]:
            aa = d['long_label']
            rq = d['raw_data'][0][0]
            last_value = d['raw_data'][0][1]

        # root = etree.HTML(html.text)
        #
        # for r in root.xpath('//div[@class="company-segmetation"]/a'):
        #     title = r.xpath('.//@href')[0]
        #     url_child = "https://www.jobui.com" +title
        #
        #     self.get_detail(url_child)

    def get_detail(self, url_child):
        html_child = HttpUtils.do_request("GET", url_child, self.headers, "")
        root_child = etree.HTML(html_child.text)
        dict_data = dict()
        dict_data['公司名称'] = root_child.xpath('//h1[@class="company-banner-title"]/a/text()')[0]
        dict_data['地址'] = "".join(root_child.xpath('//span[@class="edit-address"]/text()'))

        # 保存数据
        HttpUtils.dict_to_csv("公司名称", dict_data)

    # def insertItem(self, tableName, data):
    #     my_set = db[tableName]
    #     my_set.insert_one(data)

if __name__ == '__main__':
    # conn = MongoClient('127.0.0.1', 27017)
    # db = conn["Test"]
    test = TestRequest()

    for i in range(1, 11):
        print(f"============第【{i}】页=============")
        test.get_contents(i)
